<?php
class Library_Model_ChuDeMapper extends Library_Model_DataMapperAbstract
{
	private static $_instance = null;
	private $_paginator = null ;
	
	public function searchChuDeById($id)
	{
		$db = $this->getDb();
		$selectChude = $db->select()
								->from('tbl_chude')
								->where('MaChuDe = ?',$id);
		$chude = $db->fetchRow($selectChude);
		return $this->_populate($chude);
	}
	public function tintucList()
	{
		$db = $this->getDb();
		$selectChude = $db->select()
						  ->from('tbl_chude');
		$chudes = $db->fetchAll($selectChude);
		
		$chudeObjArray = array();
		foreach($chudes as $chude){
			$tintucMapper = Library_Model_TinTucMapper::getInstance();
			$tintuc = $tintucMapper->searchByMachude($chude['MaChuDe']);
			$chude['tbl_tintuc'] = $tintuc;
			$chudeObjArray[] = $this->_populate($chude);
		}
		
		return $chudeObjArray;
	}
	public function chudeList()
	{
		$db = $this->getDb();
		$selectChude = $db->select()
						->from('tbl_chude');
		$chudes = $db->fetchAll($selectChude);
	
		$chudeObjArray = array();
		foreach($chudes as $chude){
			$chudeObjArray[] = $this->_populate($chude);
		}
	
		return $chudeObjArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_ChuDe();
		$obj->setId($data['MaChuDe']);
		$obj->setTenchude($data['TenChuDe']);
		$obj->setTintuc($data['tbl_tintuc']);
		
		return $obj;		
	}
	
	protected function _insert(Library_Model_DomainObjectAbstract $obj){	
		$db = $this->getDb();
		$data =array(
				'MaChuDe' => $obj->getId(),
				'TenChuDe' => $obj->getTenChuDe()				
				);
		$db->insert('tbl_chude',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data =array(
				'MaChuDe' => $obj->getId(),
				'TenChuDe' => $obj->getTenChuDe()
				);
		$where = $db->quoteInto('MaChuDe = ?', $obj->getId());
		$db->update('tbl_chude',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaChuDe = ?',$id);
		$db->delete('tbl_chude',$where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
	
	}